package com.zh.note.demo.collection;


import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/4/2 10:49
 * @description: create it
 */
public class ListDemo {
    public static void main(String[] args) {
        /**
         * 创建list四种方式
         */
        // List<String> list = new ArrayList<>();
        // List<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
        // List<String> unmodifiableList = Collections.unmodifiableList(Arrays.asList("1", "2", "3"));
        // List<Integer> synchronizedList = Collections.synchronizedList(Arrays.asList(1, 4, 5, 6));
        /**
         *
         * ConcurrentModificationException
         */
        // List<Integer> list = new ArrayList<>();
        // list.add(1);
        // list.add(2);
        // for (Integer num : list) {
        //     if (num == 2) {
        //         list.remove(num);
        //     }
        // }

        String line = "4 4 2 1 2 1 4 3 3 3 4 4";
        // String line = "1 3 3 3 2 1 5";
        String[] split = line.split(" ");
        Map<Integer, Integer> map = new HashMap<>();
        for (String s : split) {
            int num = Integer.parseInt(s);
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(entries);
        System.out.println(list);

        // 排序，自定义排序规则
        list.sort(new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {

                if (!o2.getValue().equals(o1.getValue())) {
                    return o2.getValue() - o1.getValue();
                } else {
                    // value 值相等则按照key降序排列
                    return o2.getKey() - o1.getKey();
                }

            }
        });
        System.out.println(list.toString());
    }
}
